﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MP3Ganizer.Common.Interfaces.MassEditor;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Composite.Events;
using MP3Ganizer.Common.BaseObject;
using MP3Ganizer.Common;
using MP3Ganizer.Modules.MassEditor.Views;
using MP3Ganizer.Modules.MassEditor.ViewModels;
using MP3Ganizer.Events.User;

namespace MP3Ganizer.Modules.MassEditor.Controllers
{
    public class MassEditorController : BaseModuleController,IMassEditorController
    {
        public MassEditorController(IUnityContainer container, IRegionManager regionManager, IEventAggregator eventAggregator)
            : base(container, regionManager, eventAggregator)
        {
        }
        protected override void SubscribeCompositeEvent()
        {
            _eventAggregator.GetEvent<UserActionEvent>().Subscribe(OnUserAction);

        }

        private void OnUserAction(UserActionEventArgs args)
        {
            IMassEditorView view =  _regionManager.Regions[RegionNames.Right].Views.FirstOrDefault(item => item is IMassEditorView) as IMassEditorView;
                if (view == null)
                    return;
            IMassEditorViewModel viewModel = view.ViewModel;

            if (args.Action == Enums.UserAction.LoadedFileTree)
            {
                viewModel.ArtistsEditorViewModel.Initialize();
                viewModel.AlbumsEditorViewModel.Initialize();
            }
        }

        public override void Run()
        {
             _regionManager.RegisterViewWithRegion(RegionNames.Right, () => RegisterView());
        }

        private IMassEditorView RegisterView()
        {
            MassEditorView view = _container.Resolve<MassEditorView>();
            MassEditorViewModel viewModel = (MassEditorViewModel)view.DataContext;
            //viewModel.Initialize();

            return view;
        } 
    }
}
